package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerProblemListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerProblem;
import com.isunland.managesystem.entity.CustomerProblemOriginal;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/2/22 18:24
 */
public class CustomerProblemListFragment extends BaseListFragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.CustomerProblemListFragment.EXTRA_CONTENT";
  protected static final String EXTRA_MAINSTATUS =
      "com.isunland.managesystem.ui.CustomerProblemListFragment.EXTRA_MAINSTATUS";

  private final int REQUEST_QUERY = 0;

  private ArrayList<CustomerProblem> mList;
  private String dataStatusCode;
  private String dataStatusName;
  private String regDateCode;
  private String regDateName;
  private String customerName;
  private String contractId;
  private String contractName;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = ((BaseVolleyActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {
      actionBar.setTitle(R.string.customer_problem);
    }
    dataStatusCode = "undeal";
    dataStatusName = "待处理";
    regDateCode = "";
    regDateName = "全部";
    customerName = "";
    contractId = "";
    contractName = "";
    mList = new ArrayList<>();
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDividerHeight(0);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;

      //查询
      case R.id.menu_item_query:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(dataStatusName);
        queryConten.setExtCode1(dataStatusCode);
        queryConten.setExtName2(regDateName);
        queryConten.setExtCode2(regDateCode);
        queryConten.setExtName3(customerName);
        queryConten.setExtName4(contractName);
        queryConten.setExtCode4(contractId);

        Intent query = new Intent(getActivity(), CustomerProblemQueryActivity.class);
        query.putExtra(CustomerProblemQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_QUERY);

        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("dataStatus", dataStatusCode);
    params.put("dateRange", regDateCode);
    params.put("customerName", customerName);
    params.put("contractId", contractId);
    return params;
  }

  @Override public String getUrl() {
    return ApiConst.URL_CUSTOMER_PROBLEM_LIST;
  }

  @Override public void resolveResponse(String json) {
    CustomerProblemOriginal original = new Gson().fromJson(json, CustomerProblemOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      CustomerProblemListAdapter mAdapter = new CustomerProblemListAdapter(mActivity, mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    CustomerProblem content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), CustomerProblemDetailActivity.class);
    intent.putExtra(EXTRA_CONTENT, content);
    startActivity(intent);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(CustomerProblemQueryFragment.EXTRA_CONTENT);
      dataStatusName = queryConten.getExtName1();
      dataStatusCode = queryConten.getExtCode1();
      regDateName = queryConten.getExtName2();
      regDateCode = queryConten.getExtCode2();
      customerName = queryConten.getExtName3();
      contractName = queryConten.getExtName4();
      contractId = queryConten.getExtCode4();
    }

    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
